package com.gamota.mobi.adapter;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.androidquery.AQuery;

import com.gamota.mobi.ItemBrokerActivity;
import com.gamota.mobi.R;
import com.gamota.mobi.background.BackgroundTasks;
import com.gamota.mobi.fragment.BaseFragment;
import com.gamota.mobi.model.BannerItem;
import com.gamota.mobi.util.Constants;
import com.gamota.mobi.view.ScaleImageView;

public class BannerSlideFragment extends BaseFragment {
	
	private BannerItem banner;
	private BackgroundTasks bgTask;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mParent = inflater.inflate(R.layout.slide_banner_item, null);
		AQuery aq = new AQuery(mParent);
		ScaleImageView imageView = (ScaleImageView) mParent.findViewById(R.id.banner_image);
		aq.id(imageView).image(banner.getImages(), true, true, 0, R.drawable.ic_loading, null, AQuery.FADE_IN).clicked(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				bgTask.clickBanner(banner.getUrl());
				if(banner.getSlug().startsWith("market")||banner.getSlug().startsWith("http")){
					try {
					    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(banner.getSlug())));
					} catch (android.content.ActivityNotFoundException anfe) {
					}
				}else{
					Intent intent = new Intent(getActivity(), ItemBrokerActivity.class);
					intent.putExtra(Constants.STORE_KEY, "apps");
					intent.putExtra(Constants.PREVIOUS_SCREEN, "/Newsfeed");
					intent.putExtra(Constants.SLUG, banner.getSlug());
					intent.putExtra(Constants.KEY_REFERER, "ads");
					intent.putExtra(Constants.FROM, 2);
					intent.putExtra(Constants.AUTO_INSTALL, banner.getAutoInstall());
					startActivity(intent);
					
				}
				
			}
		});
		return mParent;
	}

	@Override
	protected void initVariables() {
		// TODO Auto-generated method stub
		bgTask = new BackgroundTasks(mContext);
		banner = (BannerItem) getArguments().getSerializable(Constants.IMAGES);
		System.err.println(banner.getName() + " " + banner.getImages());
	}

	@Override
	protected void initActions() {
		// TODO Auto-generated method stub
		
	}

}
